package work;

import ICTCLAS.I3S.AC.ICTCLAS50;

public class WordSeg {
//	private final String SegFileName = "seg.c110722.gz";
//	public String[] segmentFDU(String str) throws Exception{
//		CWSTagger tag = new CWSTagger(SegFileName);
//		String result = tag.tag(str);
//		System.out.println(result);
//		return result.split(" ");
//	}
	public String[] segment(String str){
		return segmentICTCLAS(str);
	}
	
	private String[] segmentICTCLAS(String str){
		return testICTCLAS_ParagraphProcess(str);
	}
	
	private String[] testICTCLAS_ParagraphProcess(String sInput)
	{
		try
		{
			ICTCLAS50 testICTCLAS50 = new ICTCLAS50();
			String argu = ".";
			//初始化
			if (testICTCLAS50.ICTCLAS_Init(argu.getBytes("UTF-8")) == false)
			{
				System.out.println("Word Segment Init Fail!");
				return null;
			}


			//设置词性标注集(0 计算所二级标注集，1 计算所一级标注集，2 北大二级标注集，3 北大一级标注集)
			testICTCLAS50.ICTCLAS_SetPOSmap(0);


			//导入用户词典前分词
			byte nativeBytes[] = testICTCLAS50.ICTCLAS_ParagraphProcess(sInput.getBytes("UTF-8"), 0, 0);//分词处理
//			System.out.println(nativeBytes.length);
			String nativeStr = new String(nativeBytes, 0, nativeBytes.length, "UTF-8");
//			System.out.println(nativeStr);//打印结果
			
//
//			//导入用户字典
//			int nCount = 0;
//			String usrdir = "userdict.txt"; //用户字典路径
//			byte[] usrdirb = usrdir.getBytes();//将string转化为byte类型
//			//导入用户字典,返回导入用户词语个数第一个参数为用户字典路径，第二个参数为用户字典的编码类型
//			nCount = testICTCLAS50.ICTCLAS_ImportUserDictFile(usrdirb, 0);
//			System.out.println("导入用户词个数" + nCount);
//			nCount = 0;


//			//导入用户字典后再分词
//			byte nativeBytes1[] = testICTCLAS50.ICTCLAS_ParagraphProcess(sInput.getBytes("GB2312"), 2, 0);
//			System.out.println(nativeBytes1.length);
//			String nativeStr1 = new String(nativeBytes1, 0, nativeBytes1.length, "GB2312");
//			System.out.println("导入用户词典后的分词结果： " + nativeStr1);
//			//保存用户字典
//			testICTCLAS50.ICTCLAS_SaveTheUsrDic();
			//释放分词组件资源
			testICTCLAS50.ICTCLAS_Exit();
			return nativeStr.split(" ");
		}
		catch (Exception ex)
		{
			return null;
		}
		
	}
	
}
